package com.zx.web.service.impl;

import com.zx.bean.Cart;
import com.zx.bean.Goods;
import com.zx.web.dao.CartDao;
import com.zx.web.dao.GoodsDao;
import com.zx.web.dao.impl.CartDaoImpl;
import com.zx.web.dao.impl.GoodsDaoImpl;
import com.zx.web.service.CartService;

import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.service.impl
 * @Description:
 * @Date 2022/9/14 16:50
 */
public class CartServiceImpl implements CartService {
    CartDao dao = new CartDaoImpl();
    @Override
    public Cart findCart(int userId, int goodsid) {
        return dao.findCart(userId,goodsid);
    }

    @Override
    public boolean updateCart(Cart cart) {
        return dao.update(cart);
    }

    @Override
    public boolean add(Cart cart) {
        return dao.add(cart);
    }

    @Override
    public List<Cart> findCartByUserId(int userId) {
        List<Cart> carts = dao.findCartByUserId(userId);
        GoodsDao goodsDao = new GoodsDaoImpl();
        if (carts.size() > 0) {
            //通过pid查询商品
            for (Cart cart : carts) {
                Goods goodsDetail = goodsDao.findGoodsDetail(cart.getPid());
                cart.setGoods(goodsDetail);
            }
        }
        return carts;
    }

    @Override
    public boolean deleteCart(int userId, int goodsId) {
        return dao.deleteCart(userId,goodsId);
    }

    @Override
    public boolean clearCartById(int userId) {
        return dao.clearCartById(userId);
    }
}
